package spr;

import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.Rules;
import robocode.ScannedRobotEvent;
import java.awt.Color;

/**
 * RastaBot_1.1 : A small improvement of RastaBot_1.0.
 * 
 * @author Shaun Ramento
 * @version 1.1
 * 
 */

public class RastaBot extends Robot {

  // Don't turn if there's a robot there
  boolean peek;
  // Amount of pixels to move
  double moveAmount;
  // When to dodge the next bullet
  int counter = 0;
  // To prevent any diagonal movement.
  int diagonalProof = 0;
  double fireMax = Rules.MAX_BULLET_POWER;

  /**
   * Moves around the wall counter-clockwise.
   */
  public void run() {

    // Representing reggae colors
    setBodyColor(Color.red);
    setGunColor(Color.yellow);
    setRadarColor(Color.green);
    setBulletColor(Color.green);
    setScanColor(Color.yellow);

    // Gets the max movement to the wall.
    moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());

    peek = false;

    // Turn the gun to turn left 90 degrees.
    turnLeft(getHeading() % 90);
    ahead(moveAmount);
    peek = true;

    turnGunLeft(90);
    turnLeft(90);

    while (true) {
      // Checks to see if enemy is on the next wall.
      peek = true;
      // Move up the wall
      ahead(moveAmount);
      // Don't look now
      peek = false;
      // Turn to the next wall.
      turnLeft(90);

      if (diagonalProof % 2 == 0) {
        turnLeft(getHeading() % 90);
      }
      diagonalProof++;
    }
  }

  /**
   * Fires a bullet at the scanned enemy. The fire power differs from the distance of the enemy.
   * 
   * @param enemy ScannedRobotEvent
   */
  public void onScannedRobot(ScannedRobotEvent enemy) {

    moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());

    if (enemy.getDistance() <= 500) {
      fire(fireMax);
      out.println(fireMax);
      // Scans to see if the enemy is on the next wall.
      if (peek) {

        scan();
      }
    }
  }

  /**
   * Moves forward if enemy is in front. Moves forward if enemy is behind.
   * 
   * @param enemy HitRobotEvent
   */
  public void onHitRobot(HitRobotEvent enemy) {

    moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());

    if (enemy.getBearing() > -90 && enemy.getBearing() < 90) {
      turnRight(enemy.getBearing() + 90);
      fire(fireMax);
    }

    else if (enemy.getBearing() <= -90 && enemy.getBearing() >= 90) {
      turnLeft(enemy.getBearing() + 90);
      fire(fireMax);
    }

  }

  /**
   * Moves after it gets hit the first time and every other time.
   * 
   * @param enemy HitByBulletEvent
   */
  public void onHitByBullet(HitByBulletEvent enemy) {

    moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());

    if ((enemy.getPower() != fireMax)) {
      turnRight(enemy.getBearing() + 90);
      fire(fireMax);
    }

    if (enemy.getPower() == fireMax) {
      turnLeft(45);
    }
    counter++;
  }

  /**
   * For testing to see that RastaBot fires with max power.
   */
  public void fireMax() {

    fire(fireMax);
    out.println(fireMax);
  }

}